ActivationGrad ================= 激活函数梯度计算算子系列。该系列算子根据激活函数的输入(或输出)以及上一层传回的梯度,计算并输出当前层的梯度。 .. math:: \text{通用公式:}\quad dst_i = src0_i \cdot f'(src1_i) 其中 :math:`src0` 为梯度输入(Output Gradient),:math:`src1` 为激活函数的原始输入或输出(取决于具体激活函数类型),:math:`f'` 为激活函数的导数。 包含算子列表: - **ReluGrad**: ReLU 激活梯度。 - **Relu6Grad**: ReLU6 激活梯度。 - **LeakyReluGrad (l_relu_grad)**: Leaky ReLU 激活梯度,需传入参数 ``alpha``。 - **SigmoidGrad**: Sigmoid 激活梯度。 - **TanhGrad**: Tanh 激活梯度。 - **HSigmoidGrad**: Hard Sigmoid 激活梯度。 - **HSwishGrad**: Hard Swish 激活梯度。 - **GeluGrad**: GELU 激活梯度。 - **EluGrad**: ELU 激活梯度,需传入参数 ``alpha``。 - **SoftplusGrad**: Softplus 激活梯度。 - **HardShrinkGrad**: Hard Shrink 激活梯度,需传入参数 ``lambd``。 - **SoftshrinkGrad**: Softshrink 激活梯度,需传入参数 ``lambd``。 输入: - **src0** - 梯度输入数据地址。 - **src1** - 原始输入/输出数据地址。 - **length** - 计算长度。 - **alpha / lambd (float, 可选)** - 特定激活函数所需的系数。 - **core_mask (int, 可选)** - 核掩码(仅适用于共享存储版本)。 输出: - **dst** - 梯度计算结果输出地址。 支持平台: ``FT78NE`` ``MT7004`` .. note:: - FT78NE 仅支持 fp32。 - MT7004 支持 fp32, fp16。 - 对于不同的算子,``src1`` 的含义可能不同(例如 SigmoidGrad 通常使用前向传播的输出作为 src1,而 ReluGrad 使用前向传播的输入作为 src1),需确保上层传入地址正确。 **共享存储版本:** .. c:function:: void fp_relu_grad_s(float* src0, float* src1, float* dst, int length, int core_mask) .. c:function:: void hp_relu_grad_s(half* src0, half* src1, half* dst, int length, int core_mask) .. c:function:: void fp_relu6_grad_s(float* src0, float* src1, float* dst, int length, int core_mask) .. c:function:: void fp_l_relu_grad_s(float* src0, float* src1, float* dst, int length, float alpha, int core_mask) .. c:function:: void fp_sigmoid_grad_s(float* src0, float* src1, float* dst, int length, int core_mask) .. c:function:: void fp_tanh_grad_s(float* src0, float* src1, float* dst, int length, int core_mask) .. c:function:: void fp_h_sigmoid_grad_s(float* src0, float* src1, float* dst, int length, int core_mask) .. c:function:: void fp_h_swish_grad_s(float* src0, float* src1, float* dst, int length, int core_mask) .. c:function:: void fp_gelu_grad_s(float* src0, float* src1, float* dst, int length, int core_mask) .. c:function:: void fp_elu_grad_s(float* src0, float* src1, float* dst, int length, float alpha, int core_mask) .. c:function:: void fp_softplus_grad_s(float* src0, float* src1, float* dst, int length, int core_mask) .. c:function:: void fp_hard_shrink_grad_s(float* src0, float* src1, float* dst, int length, float lambd, int core_mask) .. c:function:: void fp_softshrink_grad_s(float* src0, float* src1, float* dst, int length, float lambd, int core_mask) **C调用示例:** .. code-block:: c :linenos: :emphasize-lines: 11 //FT78NE示例(共享存储) #include #include "78NE/utils.h" int main(int argc, char* argv[]) { float *src0 = (float *)0xA0000000; // 梯度输入在共享存储 float *src1 = (float *)0xA1000000; // 原始数据在共享存储 float *dst = (float *)0xB0000000; // 结果输出到共享存储 int length = 1024; int core_mask = 0xff; fp_relu_grad_s(src0, src1, dst, length, core_mask); return 0; } **私有存储版本:** .. c:function:: void fp_relu_grad_p(float* src0, float* src1, float* dst, int length) .. c:function:: void hp_relu_grad_p(half* src0, half* src1, half* dst, int length) .. c:function:: void fp_relu6_grad_p(float* src0, float* src1, float* dst, int length) .. c:function:: void fp_l_relu_grad_p(float* src0, float* src1, float* dst, int length, float alpha) .. c:function:: void fp_sigmoid_grad_p(float* src0, float* src1, float* dst, int length) .. c:function:: void fp_tanh_grad_p(float* src0, float* src1, float* dst, int length) .. c:function:: void fp_h_sigmoid_grad_p(float* src0, float* src1, float* dst, int length) .. c:function:: void fp_h_swish_grad_p(float* src0, float* src1, float* dst, int length) .. c:function:: void fp_gelu_grad_p(float* src0, float* src1, float* dst, int length) .. c:function:: void fp_elu_grad_p(float* src0, float* src1, float* dst, int length, float alpha) .. c:function:: void fp_softplus_grad_p(float* src0, float* src1, float* dst, int length) .. c:function:: void fp_hard_shrink_grad_p(float* src0, float* src1, float* dst, int length, float lambd) .. c:function:: void fp_softshrink_grad_p(float* src0, float* src1, float* dst, int length, float lambd) **C调用示例:** .. code-block:: c :linenos: :emphasize-lines: 11 //MT7004 示例 #include int main(int argc, char* argv[]) { float *src0 = (float *)0x10000000; // 私有存储空间地址 float *src1 = (float *)0x10001000; float *dst = (float *)0x10002000; int length = 139; float alpha = 0.01f; fp_l_relu_grad_p(src0, src1, dst, length, alpha); return 0; }